(function(K) {
	
var Assert = YAHOO.util.Assert;

function getDependencies(callback) {
    K.classLoader().
    requires("tk.k90tools.lang.Function").
    load(function(I) { callback(I); });
}
    
K.classLoader(K.data("testConfig").basePath).
provides("tk.k90tools.lang.FunctionTest").
load(function(I) {

    return function() {
        var test = new YAHOO.tool.TestCase({
            name : "tk.k90tools.lang.FunctionTest",
            
            testApply : function() {
                getDependencies(function(I) {
                	test.resume(function() {
                		var args = [10, 20, 30];
                		var scope = {init : 5};
                		var sum = function() {
                			var sum = this.init;
                			for (var i = 0, l = arguments.length; i < l; i++) {
                				sum += arguments[i];
                			}
                			return sum;
                		}
                		
                		var total = I.Function.apply(sum, scope, args);
                		Assert.areEqual(total, 65);
                	});
                });
                
                this.wait();
            	
            },
            
            testDelegate : function() {
                getDependencies(function(I) {
                    test.resume(function() {
                    	var init = false;
                    	var delegateFunction = function() {
                    		init = !init;
                    		return function() {
                                var sum = this.init;
                                for (var i = 0, l = arguments.length; i < l; i++) {
                                    sum += arguments[i];
                                }
                                return sum;
                            }
                    	};
                    	
                    	
                    	Assert.isFalse(init);
                    	var delegate = I.Function.delegate(delegateFunction);
                        var args = [10, 20, 30];
                        var scope = {init : 5};                     
                    	
                        Assert.isFalse(init);
                        
                        var total = delegate.apply(scope, args);
                        Assert.areEqual(65, total);
                        
                        Assert.isTrue(init);
                        total = delegate.apply(scope, args);
                        Assert.isTrue(init);
                        Assert.areEqual(65, total);
                    });
                });
                
                this.wait();            	
            },
            
            
            testBind : function() {
                getDependencies(function(I) {
                    test.resume(function() {
                    	
                    	var o = {};
                    	var bindFunction = function(a,b,c,d) {
                    		Assert.areSame(o, this);
                    		
                    		Assert.areSame(a, 1);
                    		Assert.areSame(b, 2);
                    		Assert.areSame(c, 3);
                    		Assert.areSame(d, 4);
                    	};
                    	
                    	var boundFunction = I.Function.bind(bindFunction, o, 1,2,3,4);
                    	boundFunction();
                    });
                });
                
                this.wait();            	
            }
    
        });
        
        return test;
    }
});

})(k90tools);